package qianfeng.com.meijiadaka;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.slider.library.SliderLayout;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import qianfeng.com.meijiadaka.adapter.CommodityAdapter;
import qianfeng.com.meijiadaka.bean.detail.CommodityBean;

public class CommodityDtailsActivity extends AppCompatActivity {

    private String path = "http://mall.naildaka.com/vapi/v3/mall/commodity/detail?uid=&&commodityId=";
    private int commodityId;

    //主布局listView
    @BindView(R.id.commodity_detail_listView)
    ListView mListView;
    private View mPopView,mHeadView;
    ImageView image_play,pop_image_gb,pop_iv;
    private TextView pop_tv1,pop_tv2,pop_count;
    ProgressBar pb;
    private PopupWindow popupWindow;
    //轮播一
    private SliderLayout mSl;
    private ImageView headImage,pinlun_image;
    private TextView tv_name,tv_thismoney,tv_oldmoney,tv_freight,tv_sales,tv3,tv_pinlun,tv_nickname,tv_commend,tv_goodpin,tv_all_evaluate;
    private LinearLayout ll_pinlun,ll_product_present;
    private VideoView mVideoView;
    private RecyclerView mRecyclerView;
    private CommodityBean mBean;
    private CommodityAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        Vitamio.isInitialized(this);
        setContentView(R.layout.xtzdemo);
        ButterKnife.bind(this);
        commodityId = getIntent().getIntExtra("commodityId",0);
        initHead();
        loadData();
    }

    private void initHead() {
        mHeadView = LayoutInflater.from(this).inflate(R.layout.commodity_detail_lv_head,null);
        headImage = (ImageView) findViewById(R.id.commodity_detail_head_image);
        pb = (ProgressBar) mHeadView.findViewById(R.id.loading_process_dialog_progressBar1);
        image_play = (ImageView) mHeadView.findViewById(R.id.specia_details_play_iv1); //播放

        tv_name = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv);
        tv_thismoney = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv_thismoney);
        tv_oldmoney = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv_oldmoney);
        tv_freight = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv_freight); //运费
        tv_sales = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv_sales); //销量
        tv3 = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv3);  //标签
        tv_pinlun = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_tv_pinlun);  //评论

        tv_nickname = (TextView) mHeadView.findViewById(R.id.specia_list4_item_nickname);  //昵称
        tv_commend = (TextView) mHeadView.findViewById(R.id.specia_list4_item_tv_commend);  //老师好厉害
        tv_goodpin = (TextView) mHeadView.findViewById(R.id.commodity_detail_goodpin);  //好评
        tv_all_evaluate = (TextView) mHeadView.findViewById(R.id.commodity_detail_item1_all_evaluate); //查看全部评价
        pinlun_image = (ImageView) mHeadView.findViewById(R.id.commodity_head_ivhead);
        ll_pinlun = (LinearLayout) mHeadView.findViewById(R.id.ll_commodity_detail_pinlun); //评论布局线性
        ll_product_present = (LinearLayout) mHeadView.findViewById(R.id.ll_product_present);  //产品介绍

        mVideoView = (VideoView) mHeadView.findViewById(R.id.commodity_detail_head_videoView);

        mSl = (SliderLayout) findViewById(R.id.slider_commodity);
        mBean = new CommodityBean();

    }
    //加载网络数据
    private void loadData(){
        RequestParams rp = new RequestParams(path+commodityId);
        rp.setConnectTimeout(3000);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mBean = new Gson().fromJson(result,CommodityBean.class);
                Picasso.with(CommodityDtailsActivity.this).load(mBean.getResult().getCommodityIcon()).into(headImage);
//                DefaultSliderView defaultSliderView = new DefaultSliderView(CommodityDtailsActivity.this);
//                defaultSliderView.image(mBean.getResult().getCommodityIcon());
//                //轮播点击事件
//                defaultSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
//                    @Override
//                    public void onSliderClick(BaseSliderView slider) {
//                        Toast.makeText(CommodityDtailsActivity.this,"点击了", Toast.LENGTH_SHORT).show();
////                        startActivity(new Intent(getActivity(), SpeciaDetailsActivity.class));
//                    }
//                });
//                mSl.addSlider(defaultSliderView);

                Log.e( "onSuccess: mBean==",mBean.toString());
                tv_name.setText(mBean.getResult().getCommodityName());
                tv_thismoney.setText(jisuan(mBean.getResult().getMinPrice()));
                tv_oldmoney.setText(jisuan(mBean.getResult().getMaxDiscount()));
                tv_sales.setText("销量："+mBean.getResult().getSaleCount());

                if (mBean.getResult().getLastComment()!=null){
                    ll_pinlun.setVisibility(View.VISIBLE);
                    Picasso.with(CommodityDtailsActivity.this).load(mBean.getResult().getLastComment().getAvatar()).into(pinlun_image);
                    tv_pinlun.setText("评论("+mBean.getResult().getCommentNum()+")");
                    if (mBean.getResult().getLastComment().getScore().equals("GOOD")){
                        tv_goodpin.setText("好评");
                    }else {
                        tv_goodpin.setText("差评");
                    }
                    tv_nickname.setText(mBean.getResult().getLastComment().getNickname());
                    tv_commend.setText(mBean.getResult().getLastComment().getContent());
                    tv_all_evaluate.setText("查看全部评价");
                }else {
                    ll_pinlun.setVisibility(View.GONE);
                    tv_all_evaluate.setText("暂时没有评价");
                }

                if (mBean.getResult().getVideoUrl()!=null){
                    ll_product_present.setVisibility(View.VISIBLE);
                    //设置视频播放
                    play(mBean.getResult().getVideoUrl());
                }
                mListView.addHeaderView(mHeadView);
                adapter = new CommodityAdapter(CommodityDtailsActivity.this,mBean);
                mListView.setAdapter(adapter);

                pop_tv1.setText("");
                pop_tv2.setText("");
                pop_count.setText("");



            }

            private String jisuan(int a){
                int b = a;
                int c = b/100;
                return c+".00";
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e( "onError: ","错误" );
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("onSuccess: ","完成" );
            }

            @Override
            public void onFinished() {
                Log.e("onFinished: ","结束" );
            }
        });
    }

    //播放视频
    private void play(String path) {
        mVideoView.setVideoPath(path);   //设置视频网络地址
        // mVideoView.setVideoURI(Uri.parse(path)); //也可以是本地,也可以是网络地址
        final MediaController mediaController = new MediaController(this);
        mediaController.setVisibility(View.GONE);

        mVideoView.setMediaController(mediaController); //设置媒体控制器
        //  mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_STRETCH, 0);   //设置视频的缩放参数,这里设置为拉伸
        mVideoView.requestFocus();
        //视频播放器的准备,此时播放器已经准备好了,此处可以设置一下播放速度,播放位置等等

        //mVideoView.start();
        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(final MediaPlayer mp, int what, int extra) {
                switch (what) {
                    //开始缓冲
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                        pb.setVisibility(View.VISIBLE);
                        mp.pause();
                        break;
                    //缓冲结束
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                        pb.setVisibility(View.GONE);
                        image_play.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                image_play.setVisibility(View.GONE);
                                mp.start();
                            }
                        });
                        break;
                    //正在缓冲
                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
                        break;
                }
                return true;
            }
        });
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(CommodityDtailsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                //此处设置播放速度为正常速度1
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });
        //当播放完成后,从头开始
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                // mediaPlayer.seekTo(0);   //转到第一帧
                // mediaPlayer.start();     //开始播放
            }
        });
//        mGestureDetector = new GestureDetector(this, new SpeciaDetailsActivity.MyGestureListener());
    }

    //popWndow弹出窗口
    private void initPopWindow(){
        //加载弹出框的布局
        mPopView = LayoutInflater.from(this).inflate(
                R.layout.popwindow_shopcar, null);
        pop_image_gb = (ImageView) mPopView.findViewById(R.id.commodity_shopcar_quxiao);
        pop_iv = (ImageView) mPopView.findViewById(R.id.commodity_head_ivhead);
        pop_tv1 = (TextView) mPopView.findViewById(R.id.commodity_shopcar_shanpin);
        pop_tv2 = (TextView) mPopView.findViewById(R.id.commodity_shopcar_money);
        pop_count = (TextView) mPopView.findViewById(R.id.commodity_shopcar_count);
        pop_image_gb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }


    public void click(View view){
        switch (view.getId()){
            case R.id.commodity_shopcar_jian:

                break;
            case R.id.commodity_shopcar_jia:

                break;
            case R.id.commodity_shopcar_queren:  //确认

                break;
        }
    }


}
